package com.itany.cloud.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itany.cloud.constant.ResponseCodeConstant;
import com.itany.cloud.entity.Flight;
import com.itany.cloud.exception.RequstParameterErrorException;
import com.itany.cloud.service.FlightService;
import com.itany.cloud.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @date 2025/11/6
 */
@RestController
@RequestMapping("/flight")
public class FlightController {

    @Autowired
    private FlightService flightService;

    @GetMapping
    public ModelAndView findByParams(String pageNo,String pageSize,String leaveCity,String arrivalCity,String no,String company,String leaveTime ,String arrivalTime){
        ModelAndView mav = new ModelAndView();
        Flight param = new Flight( leaveCity, arrivalCity, no, company, leaveTime , arrivalTime);
        mav.addObject("param", param);

        Page<Flight> flightPage = flightService.findByParams(pageNo,pageSize,leaveCity,arrivalCity,no,company,leaveTime,arrivalTime);
        mav.addObject("flightPage", flightPage);
        mav.setViewName("flightManage");

        return mav;
    }

    @PostMapping
    public ResponseResult addFlight(String no,String company,String model,String totalSeats,String leaveCity,String arrivalCity,String economyPrice ,String firstPrice,String leaveTime ,String arrivalTime){
        ResponseResult result = new ResponseResult();
        try {
            flightService.addFlight( no, company, model, totalSeats, leaveCity, arrivalCity, economyPrice , firstPrice, leaveTime , arrivalTime);
            result.setMessage("成功添加航班信息");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequstParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }
        return result;
    }
}
